package com.leo.train.business.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leo.train.business.dto.TrainInfoQueryDto;
import com.leo.train.business.entity.TrainInfo;
import com.leo.train.business.mapper.TrainInfoMapper;
import com.leo.train.business.service.TrainInfoService;
import com.leo.train.common.exception.BusinessException;
import com.leo.train.common.exception.BusinessExceptionEnum;
import com.leo.train.common.resp.PageResult;
import com.leo.train.common.util.SnowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TrainInfoServiceImpl implements TrainInfoService {
    @Autowired
    private TrainInfoMapper trainInfoMapper;


    /**
     * 保存
     * @param trainInfo
     */
    @Override
    public void save(TrainInfo trainInfo) {
        Long id = trainInfo.getId();
        if(id == null) {
            // 判断唯一键：列车编号+索引
            TrainInfo trainInfoExist = selectByTrainCodeAndIndex(trainInfo.getTrainCode(), trainInfo.getIndex());
            if(trainInfoExist != null) {
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_INDEX_UNIQUE_ERROR);
            }
            // 判断唯一键：列车编号+站名
            TrainInfo trainInfoExist2 = selectByTrainCodeAndName(trainInfo.getTrainCode(), trainInfo.getName());
            if(trainInfoExist2 != null) {
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_NAME_UNIQUE_ERROR);
            }
            // 新增
            trainInfo.setId(SnowUtil.getSnowflakeNextId());
            trainInfo.setCreateTime(new Date());
            trainInfo.setUpdateTime(new Date());
            trainInfoMapper.insert(trainInfo);
        } else {
            // 编辑
            trainInfo.setUpdateTime(new Date());
            trainInfoMapper.update(trainInfo);
        }
    }

    @Override
    public void delete(Long id) {
        trainInfoMapper.delete(id);
    }

    @Override
    public PageResult<TrainInfo> list(TrainInfoQueryDto trainInfoQueryDto) {
        PageHelper.startPage(trainInfoQueryDto.getPage(), trainInfoQueryDto.getPageSize());
        Page<TrainInfo> page = trainInfoMapper.list(trainInfoQueryDto);
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    public TrainInfo detail(Long id) {
        TrainInfo trainInfo =trainInfoMapper.selectById(id);
        if(trainInfo == null) {
            throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_INFO_NOT_EXIST);
        }
        return trainInfo;
    }

    @Override
    public int countStationByTrainCode(String trainCode) {
        return  trainInfoMapper.countStationByTrainCode(trainCode);
    }


    /**
     * 定义一个唯一键方法
     */
    public TrainInfo selectByTrainCodeAndIndex(String trainCode, Integer index) {
        TrainInfo trainInfo = trainInfoMapper.selectByTrainCodeAndIndex(trainCode, index);
        if (trainInfo != null) {
            return trainInfo;
        }
        return null;
    }

    /**
     * 定义一个唯一键方法
     */
    public TrainInfo selectByTrainCodeAndName(String trainCode, String name) {
        TrainInfo trainInfo = trainInfoMapper.selectByTrainCodeAndName(trainCode, name);
        if (trainInfo != null) {
            return trainInfo;
        }
        return null;
    }
}
